﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Jzy.Workflow.Node;

using Jzy.Workflow.Line;

using Frame.Lib.Core.Dict;
using Frame.Lib.Core.Base;
using Frame.Lib.Data;
using Frame.Lib.Enums;

namespace Jzy.Workflow.Flow
{
    public class FlowBO : CommonOperate<FlowInfo>
    {
        private FlowDAL _flow;

        protected override BaseDAL DAL
        {
            get
            {
                return _flow;
            }
        }

        private DictBO _dict;


        public FlowBO()
            : base("Flow")
        {
            _flow = new FlowDAL();
            _dict = new DictBO();
        }

        public FlowBO(DBHelper db)
        {
            _flow = new FlowDAL();
            _flow.dbOper = base.dbOper = db;

            _dict = new DictBO(db);
        }

        public override FlowInfo GetInfo(object id)
        {
            FlowInfo info = base.GetInfo(id);
            SetDelegate(info);
            return info;
        }

        private void SetDelegate(FlowInfo info)
        {
            info.GetStartNodeInfo = new Func<object, FlowNodeInfo>(GetStartNodeInfo);
            info.GetAllNodes = id => (new FlowNodeBO()).GetList(id);
            info.GetAllLines = id => (new FlowLineBO()).GetList(id);
            info.GetTypeName = code => _dict.GetName(DictItem.流程类型, code);
        }

        private FlowNodeInfo GetStartNodeInfo(object flowID)
        {
            object nodeID = _flow.GetStartNodeID(flowID);
            return (new FlowNodeBO()).GetInfo(nodeID);
        }

        public override DataTable GetDataTable()
        {
            //DataTable dt = base.GetDataTable();            
            //_dict.AddName(dt, Jzy.Common.DictItem.流程类型, "TypeName", "TypeCode");
           
            //return dt;

            throw new Exception("该方法已被取消");
        }

        public override List<FlowInfo> GetList()
        {
            List<FlowInfo> list =  base.GetList();
            _dict.SetName<FlowInfo>(list, DictItem.流程类型, "TypeName", "TypeCode");
            return list;
        }

       
        //public override IEnumerable<FlowInfo> GetList()
        //{
        //    IEnumerable<FlowInfo> flows = base.GetList();
        //    Dict.DictBO dict = new Jzy.Core.Dict.DictBO();
        //    dict.SetName<FlowInfo>(flows, Jzy.Common.DictItem.流程类型, "TypeName", "TypeCode");
        //    return flows;
        //}

        public List<FlowInfo> GetList(string dictCode)
        {
            List<FlowInfo> list = _flow.GetList(dictCode);
            return list;
        }

        /// <summary>
        /// 获得待办箱列表所对应的流程列表
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public List<FlowInfo> GetRequiredList(int userID)
        {
            List<FlowInfo> list = _flow.GetRequiredList(userID);
            _dict.SetName<FlowInfo>(list, DictItem.流程类型, "TypeName", "TypeCode");
            return list;
        }
    }
}
